﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MineDiscover.Interfaces;
using System.Drawing;

namespace MineDiscover
{
    public class TileDraw : ITileStyle
    {
        // Implement Interface
        public Tile Tile { get; set; }
        public PointF Point { get; set; }

        // Private Field
        private SizeF size;
        private Color color;
        private Graphics gp;
        private float sizeFont;

        public void CreateTilePaint(Graphics gp, PointF point, SizeF size, float sizeFont)
        {
            this.gp = gp;
            this.Point = point;
            this.size = size;
            this.sizeFont = sizeFont;
        }

        public void OnMouseOver()
        {
            if (Tile.TileState == TileState.Close)
            {
                this.color = Color.Black;
                Paint();
            }
        }

        public void OnPaint()
        {
            if (Tile.TileState == TileState.Close)
            {
                this.color = Color.YellowGreen;
                Paint();
            }

            //if (Tile.IsMine)
            //{
            //    this.color = Color.Blue;
            //    Paint();
            //}
        }

        public void OnMouseRightClick()
        {
            if (Tile.TileState != TileState.Open)
            {
                if (Tile.TileState == TileState.Close)
                {
                    this.color = Color.YellowGreen;
                    Paint();
                }

                if (Tile.TileState == TileState.Suspect)
                {
                    this.color = Color.Red;
                    PaintSuspect(Color.White);
                }

                if (Tile.TileState == TileState.Flag)
                {
                    this.color = Color.Brown;
                    PaintFlag(Color.White);
                }
            }
        }

        public void OnMouseLeftClick()
        {
            if (Tile.TileState == TileState.Open && Tile.TileState != TileState.Flag)
            {
                this.color = Color.Beige;
                switch (Tile.MinesArround)
                {
                    case 1:
                        PaintString(Color.Blue);
                        break;
                    case 2 :
                        PaintString(Color.Green);
                        break;
                    case 3:
                        PaintString(Color.Red);
                        break;
                    case 4:
                        PaintString(Color.DarkBlue);
                        break;
                    case 5:
                        PaintString(Color.DarkRed);
                        break;
                    case 6:
                        PaintString(Color.LightBlue);
                        break;
                    case 7:
                        PaintString(Color.Orange);
                        break;
                    case 8:
                        PaintString(Color.Ivory);
                        break;
                    default:
                        Paint();
                        break;
                }
            }
        }

        public void OnExplode()
        {
            if (Tile.IsMine)
            {
                this.color = Color.Blue;
                PaintMine(Color.Red);
            }
        }

        public void OnOpenMine()
        {
            if (Tile.IsMine)
            {
                if (Tile.TileState == TileState.Flag)
                {
                    this.color = Color.Brown;
                }
                else
                {
                    this.color = Color.Blue;
                }
                PaintMine(Color.White);
            }
        }

        private void Paint()
        {
            gp.FillRectangle(new SolidBrush(color), new RectangleF(Point, size));
            gp.DrawRectangle(new Pen(Brushes.White, 1), Point.X, Point.Y, size.Width, size.Height);
        }

        private void PaintString(Color colorForeground)
        {
            gp.FillRectangle(new SolidBrush(color), new RectangleF(Point, size));
            gp.DrawRectangle(new Pen(Brushes.White, 1), Point.X, Point.Y, size.Width, size.Height);

            // Calculate paint string number
            
            float x = size.Width / 2.0f;
            float y = size.Height / 2.0f;

            PointF pointString = new PointF(Point.X + x - 10, Point.Y + y - 12);
           
            gp.DrawString(Tile.MinesArround.ToString(), new Font("Arial", sizeFont), new SolidBrush(colorForeground), pointString);
        }

        private void PaintSuspect(Color colorForeground)
        {
            gp.FillRectangle(new SolidBrush(color), new RectangleF(Point, size));
            gp.DrawRectangle(new Pen(Brushes.White, 1), Point.X, Point.Y, size.Width, size.Height);

            float x = size.Width / 2.0f;
            float y = size.Height / 2.0f;

            PointF pointString = new PointF(Point.X + x - 10, Point.Y + y - 12);

            gp.DrawString("?", new Font("Arial", sizeFont), new SolidBrush(colorForeground), pointString);
        }

        private void PaintFlag(Color colorForeground)
        {
            gp.FillRectangle(new SolidBrush(color), new RectangleF(Point, size));
            gp.DrawRectangle(new Pen(Brushes.White, 1), Point.X, Point.Y, size.Width, size.Height);

            float x = size.Width / 2.0f;
            float y = size.Height / 2.0f;

            PointF pointString = new PointF(Point.X + x - 10, Point.Y + y - 12);

            gp.DrawString("F", new Font("Arial", sizeFont), new SolidBrush(colorForeground), pointString);
        }

        private void PaintMine(Color colorForeground)
        {
            gp.FillRectangle(new SolidBrush(color), new RectangleF(Point, size));
            gp.DrawRectangle(new Pen(Brushes.White, 1), Point.X, Point.Y, size.Width, size.Height);

            float x = size.Width / 2.0f;
            float y = size.Height / 2.0f;

            PointF pointString = new PointF(Point.X + x - 10, Point.Y + y - 12);

            gp.DrawString("M", new Font("Arial", sizeFont), new SolidBrush(colorForeground), pointString);
        }
    }
}
